package com.atguigu.chapter11;

import com.atguigu.bean.WaterSensor;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.table.functions.TableAggregateFunction;
import org.apache.flink.util.Collector;

import static org.apache.flink.table.api.Expressions.$;
import static org.apache.flink.table.api.Expressions.call;

/**
 * @Author lizhenchao@atguigu.cn
 * @Date 2021/4/13 16:18
 */
public class Flink13_Function_TableAgg {
    public static void main(String[] args) {
        
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        StreamTableEnvironment tEnv = StreamTableEnvironment.create(env);
        
        DataStreamSource<WaterSensor> waterSensorStream =
            env.fromElements(new WaterSensor("sensor_1", 1000L, 10),
                             new WaterSensor("sensor_1", 2000L, 20),
                             new WaterSensor("sensor_2", 3000L, 30),
                             new WaterSensor("sensor_1", 4000L, 40),
                             new WaterSensor("sensor_1", 5000L, 50),
                             new WaterSensor("sensor_2", 6000L, 60));
        
        Table table = tEnv.fromDataStream(waterSensorStream);
        
        
        tEnv.createTemporaryFunction("top2", Top2.class);
        // 内联
        
        table
            .groupBy($("id"))
            //.flatAggregate(call(Top2.class, $("vc")).as("value", "rank"))
            .flatAggregate(call("top2", $("vc")).as("value", "rank"))
            .select($("id"), $("value"), $("rank"))
            .execute()
            .print();
        
    }
    
    public static class Top2Acc {
        public Integer first = Integer.MIN_VALUE;
        public Integer second = Integer.MIN_VALUE;
    }
    
    // 泛型一: 表示每行的值和他的排序
    public static class Top2 extends TableAggregateFunction<Tuple2<Integer, Integer>, Top2Acc> {
        
        @Override
        public Top2Acc createAccumulator() {
            return new Top2Acc();
        }
        
        // 参数1;必须是累加器
        // 参数2,3,4.. : 需要进行累加器的值
        public void accumulate(Top2Acc acc, Integer vc) {
            // 如何计算top1和top2
            if (vc > acc.first) {
                // 先把first变成second
                acc.second = acc.first;
                // 再把新值给first
                acc.first = vc;
            } else if (vc > acc.second) {
                acc.second = vc;
            }
        }
        
        // 参数1: 累加器
        // 参数2: Collector  把累加器中的值发射出去
        public void emitValue(Top2Acc acc, Collector<Tuple2<Integer, Integer>> out) {
            if (acc.first != Integer.MIN_VALUE) {
                out.collect(Tuple2.of(acc.first, 1));
            }
            
            if (acc.second != Integer.MIN_VALUE) {
                out.collect(Tuple2.of(acc.second, 2));
            }
        }
    }
    
}


